package com.jsj.utils;

import com.jsj.domain.TopFile;
import com.jsj.seafile.ServiceImpl;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 文件转换、进行查重
 */
@Component
@ConfigurationProperties(prefix = "simhash")
public class WordUtil {

    /**
     * 上传路径
     */
    private static String profile;

    public static String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        WordUtil.profile = profile;
    }

    /**
     * 获取SimHash值
     */
    public static String getSimHash(MultipartFile file) throws IOException {
        // 将MultipartFile转换为String
        String msg = fileConversion(file);
        // 获取SimHash值
        String msgSimHash = SimHashUtil.getSimHash(msg);
        return msgSimHash;
    }

    /**
     * 使用MultipartFile接收文件流
     */
    public static String fileConversion(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String fileName = file.getOriginalFilename();
        String suff = fileName.substring(fileName.lastIndexOf(".") + 1);
        String content = "";
        if (suff.equals("docx")) {
            XWPFDocument xdoc = new XWPFDocument(inputStream);
            XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
            content = extractor.getText();
            extractor.close();
        } else if (suff.equals("doc")) {
            WordExtractor ex = new WordExtractor(inputStream);
            content = ex.getText();
            ex.close();
        } else {
            content = "此文件不是word文件";
        }
        return content;
    }

    /**
     * 本地保存文件
     */
    public static int saveFile(MultipartFile file, String group) throws IOException {

        //获取上传文件名,包含后缀
        String originalFilename = file.getOriginalFilename();
        String path = getPath(group) + "/" + originalFilename;
        mkdirs(new File(getPath(group)));
        //生成保存文件
        File uploadFile = new File(path);
        //将上传文件保存到路径
        try {
            file.transferTo(uploadFile);
            return 0;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 创建文件夹顺便创建父目录
     */
    public static void mkdirs(File file) {
        if (file.exists() && file.isDirectory()) {
            return;
        }
        if (file.exists()) {
            file.delete();
            file.mkdirs();
        } else {
            file.mkdirs();
        }
    }

    /**
     * 文件真实地址
     */
    public static String getPath(String group) {
        //保存路径 = 基地址 + 分组地址
        String basePath = getProfile();
        String path = basePath + "/" + group;
        return path;
    }

    /**
     * 根据路径读取文件
     */
    public static String readWord(String path) {
        String buffer = "";
        try {
            if (path.endsWith(".doc")) {
                InputStream is = new FileInputStream(new File(path));
                WordExtractor ex = new WordExtractor(is);
                buffer = ex.getText();
            } else if (path.endsWith(".docx")) {
             /* 如果poi版本在4.0.0及以上那么可以使用注释的方式
             	OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                buffer = extractor.getText();*/
                FileInputStream fs = new FileInputStream(new File(path));
                XWPFDocument xdoc = new XWPFDocument(fs);
                XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
                buffer = extractor.getText();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }


    /**
     * seaFile保存文件
     */
    public static void saveSeaFile(MultipartFile file, String fileName, String group) throws IOException {

        String path = "D:/" + fileName;
        //生成保存文件
        File uploadFile = new File(path);
        //将上传文件保存到路径
        try {
            file.transferTo(uploadFile);
            ServiceImpl seaFile = new ServiceImpl();
            group = group + "/";
            seaFile.realupload(group, uploadFile);
            // 删除临时文件
            uploadFile.delete();
        } catch (IOException e) {
            e.printStackTrace();
            uploadFile.delete();
        }
    }

    /**
     * seaFile判断新建文件夹
     */
    public static void newSeaFile(TopFile topFile) throws IOException {
        ServiceImpl seaFile = new ServiceImpl();
        String group = "/" + topFile.getClassid().toString();
        if (!seaFile.getDetailDir(group)) {
            seaFile.testCreatDir(group);
        }
        group = "/" + topFile.getClassid().toString() + "/" + topFile.getSubtopid().toString();
        if (!seaFile.getDetailDir(group)) {
            seaFile.testCreatDir(group);
        }
        group = "/" + topFile.getClassid().toString() + "/" + topFile.getSubtopid().toString() + "/" + topFile.getProblemid().toString();
        if (!seaFile.getDetailDir(group)) {
            seaFile.testCreatDir(group);
        }
    }

}
